package cn.wolfcode.shop.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;

import cn.wolfcode.shop.domain.Product;
import cn.wolfcode.shop.domain.ProductSku;
import cn.wolfcode.shop.domain.UserLogin;
import cn.wolfcode.shop.mapper.ProductMapper;
import cn.wolfcode.shop.mapper.ProductSkuMapper;
import cn.wolfcode.shop.service.ICarService;
import cn.wolfcode.shop.util.RedisConstance;
import cn.wolfcode.shop.vo.CarVo;

@Service
@Transactional
public class CarServiceImpl implements ICarService {

	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private ProductSkuMapper productSkuMapper;
	@Autowired
	private ProductMapper productMapper;

	@Override
	public void addCar(UserLogin userLogin, CarVo carVo) {

		// 抽取出共同的判断,通过参数解析器
		//		// 1.判断token的有效性
		//		if (!StringUtils.hasLength(carVo.getToken())) {
		//			// 2 如果token非法抛出异常
		//			throw new GlobalException(CodeMsg.USER_TOKEN_ERROR);
		//		} else {
		//			// 3 没问题就通过token获取登录用户
		//			UserLogin userLogin = (UserLogin) redisTemplate.opsForValue()
		//					.get(RedisConstance.USER_CURRENT.replace("TOKEN", carVo.getToken()));
		//			// 4 如果当前登录用户为null,就提示用户登录超时,重新登录
		//			if (userLogin == null) {
		//				throw new GlobalException(CodeMsg.USER_TIME_OUT);
		//			}
		// 5 通过skuId获取整个sku对象,也同时有商品信息
		ProductSku sku = productSkuMapper.selectByPrimaryKey(carVo.getSkuId());
		// 6 拼接购物车redis的key
		//			String carKey = "car:user:USERID:product:PRODUCTID:sku:SKUID";
		String carKey = RedisConstance.CAR_KEY.replace("USERID", userLogin.getId() + "").replace("PRODUCTID", sku.getProductId() + "").replace("SKUID", sku.getId() + "");
		// 7 通过购物车的key去redis中获取key对应的购物数量
		Map car = redisTemplate.opsForHash().entries(carKey);
		// 8 如果找到购物车数据不为null并且元素不为0,那么就在原来的数量基础上,累加此次购买的数量
		if (car != null && car.size() > 0) {
			Integer number = (Integer) car.get("number");
			//				car.put("number", number + carVo.getNumber());
			redisTemplate.opsForHash().put(carKey, "number", number + carVo.getNumber());
		} else {
			// 9 否则通过sku对象中的商品id获取商品对象,新增一个购物车的数据
			Product product = productMapper.selectByPrimaryKey(sku.getProductId());
			car = new HashMap<>();
			car.put("productId", product.getId());
			car.put("skuId", sku.getId());
			car.put("number", carVo.getNumber());
			car.put("price", sku.getPrice());
			car.put("status", new Byte("1"));// 在购物车中是否有效,如果下架就是无效
			car.put("productName", product.getName());
			car.put("productImg", product.getImage());
			// sku属性值是多个的,用map存取
			HashMap<String, String> skuProperty = new HashMap<>();
			sku.getProductSkuPropertyList().forEach(productSkuProperty -> {
				skuProperty.put(productSkuProperty.getSkuProperty().getName(), productSkuProperty.getValue());
			});
			car.put("propertys", skuProperty);
			car.put("select", new Byte("1"));// 是否选中,默认选中
			redisTemplate.opsForHash().putAll(carKey, car);
		}
	}

}
